package com.proyemp.pruebaEasyService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.proyemp.objetos.parameters.prueba.EasyParameter;
import com.proyemp.objetos.presentacion.prueba.Product;
import com.proyemp.objetos.responses.prueba.EasyResponse;



public class EasyServiceDWR {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
    public List getProducts(List envio){
    	EasyParameter parametersX = (EasyParameter)envio.get(0);
        EasyResponse responseX= new EasyResponse();
 
        // si parametros vacios devuelve error, error if empty parameters 
        if (parametersX==null || parametersX.getClientID()==null || parametersX.getClientID().length()<=0){ 
            responseX.setErrorCode(10001); 
            responseX.setErrorDescription("Debe indicar ID Cliente.");
        } else {
        	//crea lista productos del cliente, fill the client product list 
            //para ejemplo en duro, for the example fixed data 
            Product[] productsListX= new Product[2]; 
            
            // vamos a conectgarnos a la base y a obtener los datos
            ejecutaConsulta();
            
            Product productX= new Product(); 
            productX.setBarCode("0001"); 
            productX.setName("Tarjeta Visa, Visa Credit Card"); 
            productsListX[0]= productX; 
     
            productX= new Product(); 
            productX.setBarCode("0002"); 
            productX.setName("aaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaa" +
            		"aaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa" + 
            		"aaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa"); 
            productsListX[1]= productX; 
     
            //respuesta exitosa, sucessfull response 
            responseX.setErrorCode(0); 
            responseX.setErrorDescription("Consulta Banco Exitosa, Succesfull Bank Query"); 
            responseX.setProducts(productsListX); 
        }
        List enviar = new ArrayList();
        enviar.add(responseX);
        return enviar; 
    }
    
    // aca se ejecuta una consulta a la base
    public List ejecutaConsulta(){
        logger.info("voy a entrar a la base");
        String url = "jdbc:postgresql://localhost/proyemp";
        List respuesta = new ArrayList();
        try {
			Class.forName("org.postgresql.Driver");
			Connection con = DriverManager.getConnection(url, "admin", "proyemp");
			Statement stmt = con.createStatement();
			ResultSet rs;
			String query = "select * from empleados";
			rs = stmt.executeQuery(query);
			
			while (rs.next()){
				logger.info("obtengo el apellido de los empleados" + rs.getString("apellido1"));
				respuesta.add(rs.getString("apellido1"));
			}
			
			stmt.execute("end");
			stmt.close();
			con.close();
			
		} catch (ClassNotFoundException e) {
			logger.info(e.getMessage());
		} catch (SQLException e) {
			logger.info(e.getMessage());
		}
		logger.info("fin de la base");
        return respuesta;
    }
    
    
    // aca se ejecuta una consulta a la base
    public void ejecutaActualizacion(){
        logger.info("voy a entrar a la base");
        String url = "jdbc:postgresql://localhost/proyemp";
        try {
			Class.forName("org.postgresql.Driver");
			Connection con = DriverManager.getConnection(url, "admin", "proyemp");
			Statement stmt = con.createStatement();
			ResultSet rs;
			String query = "select * from empleados";
			rs = stmt.executeQuery(query);
			
			while (rs.next()){
				logger.info("obtengo el apellido de los empleados" + rs.getString("apellido"));
			}
			
			stmt.execute("end");
			stmt.close();
			con.close();
			
		} catch (ClassNotFoundException e) {
			logger.info(e.getMessage());
		} catch (SQLException e) {
			logger.info(e.getMessage());
		}
        
		logger.info("fin de la base");
    }
}
